import json
import pymysql
from flask import Flask, request

app = Flask(__name__)


@app.route('/hello')
def hello_world():  # put application's code here
    return 'Hello World!'


@app.route('/user/<username>')
def show_user_profile(username):
    return "我是" + username


@app.route('/post_test', methods=['post'])
def post_test():
    return "只能发送post请求"


@app.route('/user/<int:post_id>')
def show_post(post_id):
    return "只能接收int数据,接到的数为%d" % post_id


@app.route('/op', methods=['get', 'post'])
def projects():
    res = ''
    if request.method == 'GET':
        print("进入了get方法")
        age = int(request.args.get("age", 18))
        res = {'code': 200, 'msg': 'get接口 age为%d' % age}
    elif request.method == 'POST':
        print("进入了post方法")
        age = int(request.args.get("age", 18))
        res = {'code': 201, 'msg': 'post接口 age为%d' % age}
    return json.dumps(res, ensure_ascii=False)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8081)
